package com.rstrulak.managedBeans.controllers;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.NoteAttachmentsDao;
import com.rstrulak.dao.NoteDao;
import com.rstrulak.model.Note;
import com.rstrulak.model.NoteAttachments;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("noteBean")
@Scope("request")
public class NoteBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1807913272276785559L;

	@Autowired
	private NoteDao noteDao;

	@Autowired
	private NoteAttachmentsDao noteAttachmentsDao;

	private Note note;

	private Long noteId;

	private List<NoteAttachments> noteAttachmentsList;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		if(noteId == null){
			JSFUtils.setFlashFromSession();
			noteId = (Long) JSFUtils.flashScope().get(Constants.NOTE_ID);
			JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		}

		getNoteFromDb(noteId);
		if (note != null) {
			noteAttachmentsList = noteAttachmentsDao.getNoteAttachemntsListByNote(note);
		}
	}

	public Note getNote() {
		return note;
	}

	public void setNote(Note note) {
		this.note = note;
	}

	private void getNoteFromDb(Long noteId) {
		this.note = noteDao.getById(noteId);
	}

	public List<NoteAttachments> getNoteAttachmentsList() {
		return noteAttachmentsList;
	}

	public void setNoteAttachmentsList(List<NoteAttachments> noteAttachmentsList) {
		this.noteAttachmentsList = noteAttachmentsList;
	}

}
